function DoubleLinkList() {
  function Nodes(element) {
    this.element = element;
    this.prev = null;
    this.next = null;
  }
  let length = 0;
  let head = null; //头
  let tail = null; //尾

  //任意位置插入
  this.insertNodes = (position, element) => {
    if (position > -1 && position <= length) {
      let nodes = new Nodes(element);
      let current = head,
        previous,
        index = 0;
      //头部添加
      if (position === 0) {
        if (!head) {
          head = nodes;
          tail = nodes;
        } else {
          nodes.next = current; //nodes置于头部
          current.prev = nodes; //原head的prev指向nodes
          head = nodes; //把头节点替换成noddes
        }
      } else if (position === length) {
        //末尾新增
        current = tail;
        current.next = nodes;
        nodes.prev = current;
        tail = nodes;
      } else {
        //其它情况的任意位置
        while (index++ < position) {
          previous = current;
          current = current.next;
        }
        nodes.next = current; //nodes的next指向下一个
        previous.next = nodes; //前一个节点的next指向nodes

        nodes.prev = previous; //nodes的prev指向上一个
        current.prev = nodes; //后一个节点的prev指向nodes
      }
      length++;
      return true;
    } else {
      throw new Error();
    }
  };

  //任意位置移除
  this.removeNodes = (position) => {
    if (position > -1 && position < length) {
      let current = head,
        previous,
        index = 0;
      //头部移除
      if (position === 0) {
        head = current.next;
        if (length == 1) {
          tail = null;
        } else {
          head.prev = null;
        }
      } else if(position === length-1) {  
        //最后一项移除
        current = tail;
        tail = current.prev;
        tail.next = null;
      } else {
        //其它情况的任意位置
        while(index++ < position) {
          previous = current;
          current = current.next;
        }
        previous.next = current.next;
        current.next.prev = previous;
      }
      length -- ;
      return current.element;
    } else {
      throw new Error()
    }
  };
}
